package com.lenin.metro.client;

import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.lenin.metro.shared.MapCoordinates;
import com.lenin.metro.shared.Tile;
import com.lenin.metro.shared.TileGroup;

public class TileGroupPanel extends AbsolutePanel {
	
	private TileGroup tileGroup;
	
	private int width;
	private int height;
	
	private FocusPanel focusPanel;
	
	public TileGroupPanel(TileGroup tileGroup) {
		
		this.tileGroup = tileGroup;
		setStyleName("tileGroupPanel");
		getElement().getStyle().setOverflow(Overflow.VISIBLE);
		
	}
	
	
	public FocusPanel getFocusPanel() {
		return focusPanel;
	}
	
	
	public int getWidth() {
		return width;
	}


	public void setWidth(int width) {
		this.width = width;
		setWidth(width+"px");
	}


	public int getHeight() {
		return height;
	}


	public void setHeight(int height) {
		this.height = height;
		setHeight(height+"px");
	}


	public MapCoordinates getMapCoordinates() {
		
		MapCoordinates coords = null;
		
		if(tileGroup != null) {
			coords = tileGroup.getCoordinates();
		}
		
		return coords;
		
	}
	
	
	public void build() {
		
		if(tileGroup == null) {
			return;
		}
		
		Tile[][] map = tileGroup.getTileData();
		int tileSize = tileGroup.getTileSize();
		
		int hTiles = tileGroup.getHorizontalLength();
		int vTiles = tileGroup.getVerticalLength();
		
		int tileWidth = tileSize*2;
		int tileHeight = tileSize;
		
		setHeight(tileHeight*vTiles);
		setWidth(tileWidth*hTiles);
		
		int startX = 0;
		
		int x = 0;
		int y = 0;
		
		for(int i = 0; i < vTiles * 2; i++) {
			
			x = startX;
			
			if(i % 2 != 0) {
				x += tileWidth/2;
			}
			
			for(int j = 0; j < hTiles; j++) {
				
				AbsolutePanel tile = new AbsolutePanel();
				
				tile.setWidth(tileWidth+"px");
				tile.setHeight(tileHeight+"px");
				
				if(map[i][j] != null) {
					tile.setStyleName("tile");
					tile.addStyleName("rombBg");
					tile.getElement().setInnerHTML(tileGroup.getCoordinates().getLatitude()+"");
					add(tile, x, y);
				}
				
				
				x += tileWidth;
			
			}
			
			y += tileHeight/2;
			
		}
		
	}
	
	
}
